package com.lk.lkappearance.service.impl;

import com.lk.lkappearance.common.AbstractBaseService;
import com.lk.lkappearance.mapper.UserMapper;
import com.lk.lkappearance.model.User;
import com.lk.lkappearance.service.UserService;
import com.lk.lkappearance.utills.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends AbstractBaseService implements UserService {

    @Autowired
    private UserMapper userMapper;


    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @Override
    public Map<String, Object> add(User user) {
        try {
            user.setUserId(StringUtils.getUuid());
            user.setCreateTime(new Date());
            userMapper.insertSelective(user);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return success().getResult();
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @Override
    public Map<String, Object> modify(User user) {
        try {
            user.setUpdateTime(new Date());
            userMapper.updateByPrimaryKeySelective(user);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return success().getResult();
    }

    /**
     * 删除用户
     *
     * @param user
     * @return
     */
    @Override
    public Map<String, Object> delete(User user) {
        try {
            user.setUpdateTime(new Date());
            user.setStatus("1");
            userMapper.updateByPrimaryKeySelective(user);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return success().getResult();
    }

    /**
     * @return
     */
    @Override
    public Map<String, Object> getList(User user) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<User> list = userMapper.getList(user);
            result.put("list",list);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return success().setData(result).getResult();
    }

    /**
     * @return
     */
    @Override
    public Map<String, Object> onlineNum() {
        Map<String,Object> result= new HashMap<>();
        try {
            Integer num = userMapper.onlineNum();
            result.put("num",num);
        }catch (Exception e) {
            e.printStackTrace();
        }


        return success().setData(result).getResult();
    }
}
